﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace C_homework
{
    public partial class Form1 : Form
    {
        Random random = new Random();
        int firstNumber;
        int secondNumber;
        string operation;
        int correctAnswer = 0;
        int score = 0;
        int questionNum = 0;
        int timeLeft = 10;
        int questionLeft = 10;

        public Form1()
        {
            InitializeComponent();
            StartQuestion();
        }

        public void StartQuestion()
        {
            if(questionLeft == 0)
            {
                //退出自动答题程序
            }
            else
            {
                Timelabel.ForeColor = Color.Black;
                Scorelabel.Text = $"得分为{score}";
                Questionleftlabel.Text = $"剩余题目数量为{questionLeft}";
                questionLeft--;
                timeLeft = 10;
                timer1.Start();
                //选取两个随机数
                firstNumber = random.Next(1, 101);
                secondNumber = random.Next(1, 101);

                // 随机选择加法或减法
                if (random.Next(2) == 0)
                {
                    operation = "+";
                    correctAnswer = firstNumber + secondNumber;
                }
                else
                {
                    operation = "-";
                    correctAnswer = firstNumber - secondNumber;
                }
                Questionlabel.Text = $"{firstNumber} {operation} {secondNumber} =";
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (timeLeft > 0)
            {
                timeLeft--;
                Timelabel.Text = $"剩余答题时间为 {timeLeft}";
            }
            else
            {
                timer1.Stop();
                Timelabel.Text = $"超时! 正确答案是 {correctAnswer}";
                Timelabel.ForeColor = Color.Red;
                Task.Delay(2000).ContinueWith(_ => StartQuestion(), TaskScheduler.FromCurrentSynchronizationContext());
            }
        }

        private void Submitbutton_Click(object sender, EventArgs e)
        {
            int userAnswer;
            if (int.TryParse(txtAnswer.Text, out userAnswer))
            {
                if (userAnswer == correctAnswer)
                {
                    score+=10;
                    timer1.Stop();
                    MessageBox.Show($"恭喜你答对了！正确答案是{correctAnswer}", "正确答案", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    timer1.Stop();
                    MessageBox.Show($"很遗憾你答错了！正确答案是{correctAnswer}", "错误答案", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                StartQuestion(); // 出下一题
            }
            else
            {
                MessageBox.Show("请输入一个有效的数字！");
            }
            txtAnswer.Clear(); // 清空答案框
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click_1(object sender, EventArgs e)
        {

        }

        private void label1_Click_2(object sender, EventArgs e)
        {

        }

        private void label1_Click_3(object sender, EventArgs e)
        {

        }


    }
}
